//
//  CaptureInstallListViewController.m
//  CarPark
//
//  Created by oceans on 2019/8/8.
//  Copyright © 2019 oceans. All rights reserved.
//

#import "CaptureInstallListViewController.h"
#import "CaptureInstallListCollectionViewCell.h"
#import "CaptureInstallCollectionCell.h"
#import <UIImageView+WebCache.h>
//#import <SDWebImage/UIImageView+WebCache.h>
#import "CaptureInstallViewController.h"

@interface CaptureInstallListViewController () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (strong, nonatomic) UICollectionView *collection;
@property (strong, nonatomic) NSMutableArray *array;
@property (strong, nonatomic) UIButton *finishButton;
@end

@implementation CaptureInstallListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.array = [NSMutableArray array];
    [self setupView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self requestData];
}

- (void)requestData {
    
    defweakself
    [HttpRequest getURL:kcapture_list paras:@{@"task_id": [NSString stringWithFormat:@"%@", _info[@"id"]]} showProgress:NO successful:^(id  _Nullable result) {
        NSLog(@"%@", result);
        [weakself.array removeAllObjects];
        [weakself.array addObjectsFromArray:result[@"datas"]];
        [weakself.collection reloadData];
    } failed:^(int code, NSString * _Nullable errMsg) {
        
    }];
}

- (void)setupView {
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"设备安装";
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(ScreenWidth, 100.0);
    layout.minimumLineSpacing = 0.0;
    layout.minimumInteritemSpacing = 0.0;
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, XNavigationHeight, ScreenWidth, ScreenHeight-XNavigationHeight-XBotSafeSpace-40.0) collectionViewLayout:layout];
    _collection.delegate = self;
    _collection.dataSource = self;
    [self.view addSubview:_collection];
    _collection.backgroundColor = [UIColor whiteColor];
    [_collection registerNib:[UINib nibWithNibName:@"CaptureInstallListCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CaptureInstallListCollectionViewCell"];
    [_collection registerNib:[UINib nibWithNibName:@"CaptureInstallCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"CaptureInstallCollectionCell"];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitle:@"安装完成" forState:UIControlStateNormal];
    button.frame = CGRectMake(0, ScreenHeight-XBotSafeSpace-40.0, ScreenWidth, 40.0+XBotSafeSpace);
    [button addTarget:self action:@selector(finish) forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = COLOR(102, 102, 102, 1);
    [self.view addSubview:button];
    self.finishButton = button;
}

- (void)finish {
    
    defweakself
    [HttpRequest postURL:kfinish_capture paras:@{@"id": [NSString stringWithFormat:@"%@", _info[@"id"]]} showProgress:YES successful:^(id  _Nullable result) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakself.navigationController popToRootViewControllerAnimated:YES];
        });
    } failed:^(int code, NSString * _Nullable errMsg) {
        
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return _array.count+1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.item == _array.count) {
        
        CaptureInstallCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CaptureInstallCollectionCell" forIndexPath:indexPath];
        return cell;
    }else {
        
        NSDictionary *dic = _array[indexPath.item];
        CaptureInstallListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CaptureInstallListCollectionViewCell" forIndexPath:indexPath];
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:dic[@"thumb"]]];
        cell.label1.text = dic[@"name"];
        cell.label2.text = [NSString stringWithFormat:@"状态：%@", dic[@"status_label"]];
        cell.label3.text = [NSString stringWithFormat:@"位置：%@", dic[@"parking_position"]];
        return cell;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.item == _array.count) {
        CaptureInstallViewController *con = [[CaptureInstallViewController alloc] init];
        con.info = _info;
        [self.navigationController pushViewController:con animated:YES];
    }
}

@end
